﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using Portal.CDSP;
using System.Web;

namespace Portal.CDSP
{
    public class FMenuWP : WebPart
    {

        public override string WebPartId
        {
            get
            {
                return "WebSite.FMenuWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("FTitle", "SoYT-Menu");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                     new ViewCls("TopMenuView",GetText("default","TopMenu"),0),   
                     new ViewCls("BottomMenuView",GetText("Bottom","BottomMenu"),1),   
                };
            }
        }

        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FMenuWP";
            }
        }

        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("TopMenuView"))
                {
                    return ClientRenderWebPartTop(TabModuleId, HasPorlet);
                }
                if (ViewName.Equals("BottomMenuView"))
                {
                    return ClientRenderWebPartBottom(TabModuleId, HasPorlet);
                }

                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        private string ClientRenderWebPartBottom(string TabModuleId, string HasPorlet)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));

                string Html = string.Empty;
                string Requesid = string.Empty;
                int tabid = 0;

                DataTable dtItems = new FSpecialSubjectBLL().ReadingMenuItems(TabModuleCls.belongSite);

                if (Environments.Request("gid") != null)
                {
                    Requesid = (string)Environments.Request("gid");
                    Requesid = new FSpecialSubjectBLL().ReadingGroupItemsId(Requesid);
                }
                else
                {
                    if (Environments.Request("NewsId") != null)
                    {
                        string NewsId = (string)Environments.Request("NewsId");
                        Requesid = new FSpecialSubjectBLL().ReadingGroupParentIdForNewId(NewsId);

                    }
                    else
                    {
                        if (Environments.Request("Alias") != null)
                        {
                            string AliasiD = (string)Environments.Request("Alias");
                            Requesid = new FSpecialSubjectBLL().ReadingGroupItemsId(AliasiD);
                        }
                    }
                }

                string Url = "Default.aspx";

                //********* Vẽ menu bar *************
                Html += "<div class=\"foot-nav\">\n" +
                        "<ul>";


                string CurrentUrl = HttpContext.Current.Request.RawUrl;
                bool tag = false;



                for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                {
                    string GroupNewsItemId = (string)dtItems.Rows[iIndex]["GroupNewsItemId"];
                    string GroupNewsItemName = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                    int IsGroupNews = (int)dtItems.Rows[iIndex]["IsGroupNews"];
                    //string NewsByCategoryUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemId, GroupNewsItemName, "_RunTime");
                    if (dtItems.Rows[iIndex]["JumpUrl"].ToString().Trim().Length > 0)
                        Url = (string)dtItems.Rows[iIndex]["JumpUrl"];
                    else if (IsGroupNews == 1)
                        Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemId, GroupNewsItemName, "_RunTime");
                    else Url = "#";

                    Html += "<li><a href=\"" + Url + "\">" + GroupNewsItemName + "</a></li>\n";
                    // Đúng khi với có category id                                       
                    //DataTable dtSub = new FSpecialSubjectBLL().ReadingSubMenuItems(GroupNewsItemId);
                    //if (GroupNewsItemId.Equals(Requesid))
                    //{
                    //    tag = true;
                    //    tabid = iIndex;
                    //    Html += "<li class=\"has-sub\">" +
                    //            "<a href=\"" + Url + "\" rel=\"sc" + (iIndex + 1) + "\"><span>" + GroupNewsItemName + "</span></a>";
                    //    if (dtSub.Rows.Count > 0)
                    //    {
                    //        Html += "<ul class=\"sub-ul\">";
                    //        for (int j = 0; j < dtSub.Rows.Count; j++)
                    //        {
                    //            string SubGroupNewsItemId = (string)dtSub.Rows[j]["GroupNewsItemId"];
                    //            string SubGroupNewsItemName = (string)dtSub.Rows[j][Environments.ActiveLanguage + "GroupNewsItemName"];
                    //            IsGroupNews = (int)dtSub.Rows[j]["IsGroupNews"];

                    //            string HtmlContent = "";
                    //            try
                    //            {
                    //                HtmlContent = (string)dtSub.Rows[j]["HtmlContent"];
                    //            }
                    //            catch { }

                    //            Url = (string)dtSub.Rows[j]["JumpUrl"];
                    //            if (Url.Trim().Length == 0)
                    //            {
                    //                if (IsGroupNews == 1)
                    //                    Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName, "_RunTime");
                    //                if (IsGroupNews == 0 && HtmlContent.Trim().Length > 0)
                    //                    Url = Environments.GetStaticHtmlUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName);
                    //            }
                    //            Html += "<li><a href=\"" + Url + "\">" + SubGroupNewsItemName + "</a></li>";
                    //        }
                    //        Html += "</ul>";
                    //    }
                    //    Html += "</li>";
                    //}
                    //else
                    //{
                    //    if (iIndex == 0)
                    //    {
                    //        Html += "<li {0} ><a href=\"" + Url + "\" rel=\"sc" + (iIndex + 1) + "\"><span>" + GroupNewsItemName + "</span></a>";

                    //    }
                    //    else
                    //    {
                    //        Html += "<li><a href=\"" + Url + "\" rel=\"sc" + (iIndex + 1) + "\"><span>" + GroupNewsItemName + "</span></a>";

                    //    }
                    //    if (dtSub.Rows.Count > 0)
                    //    {

                    //        Html += "<ul class=\"has-sub\">";
                    //        for (int j = 0; j < dtSub.Rows.Count; j++)
                    //        {
                    //            string SubGroupNewsItemId = (string)dtSub.Rows[j]["GroupNewsItemId"];
                    //            string SubGroupNewsItemName = (string)dtSub.Rows[j][Environments.ActiveLanguage + "GroupNewsItemName"];
                    //            IsGroupNews = (int)dtSub.Rows[j]["IsGroupNews"];

                    //            string HtmlContent = "";
                    //            try
                    //            {
                    //                HtmlContent = (string)dtSub.Rows[j]["HtmlContent"];
                    //            }
                    //            catch { }

                    //            Url = (string)dtSub.Rows[j]["JumpUrl"];
                    //            if (Url.Trim().Length == 0)
                    //            {
                    //                if (IsGroupNews == 1)
                    //                    Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName, "_RunTime");
                    //                if (IsGroupNews == 0 && HtmlContent.Trim().Length > 0)
                    //                    Url = Environments.GetStaticHtmlUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName);
                    //            }
                    //            Html += "<li><a href=\"" + Url + "\">" + SubGroupNewsItemName + "</a></li>";
                    //        }
                    //        Html += "</ul>";
                    //    }

                    //    Html += "</li>";
                    //}
                }

                if (!tag)
                {
                    Html = string.Format(Html, " class='active' ");
                }
                else
                    Html = string.Format(Html, string.Empty);
                Html +=
                            "</ul></div>";
                return Html;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        private string ClientRenderWebPartTop(string TabModuleId, string HasPorlet)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));

                string Html = string.Empty;
                string Requesid = string.Empty;
                int tabid = 0;

                DataTable dtItems = new FSpecialSubjectBLL().ReadingMenuItems(TabModuleCls.belongSite);

                if (Environments.Request("gid") != null)
                {
                    Requesid = (string)Environments.Request("gid");
                    Requesid = new FSpecialSubjectBLL().ReadingGroupItemsId(Requesid);
                }
                else
                {
                    if (Environments.Request("NewsId") != null)
                    {
                        string NewsId = (string)Environments.Request("NewsId");
                        Requesid = new FSpecialSubjectBLL().ReadingGroupParentIdForNewId(NewsId);

                    }
                    else
                    {
                        if (Environments.Request("Alias") != null)
                        {
                            string AliasiD = (string)Environments.Request("Alias");
                            Requesid = new FSpecialSubjectBLL().ReadingGroupItemsId(AliasiD);
                        }
                    }
                }

                string Url = "Default.aspx";

                //********* Vẽ menu bar *************
                Html += "<div class=\"nav-bar-inner\">" +
                        "<ul>";


                string CurrentUrl = HttpContext.Current.Request.RawUrl;
                bool tag = false;

                Html += "<li><a class=\"li-home\" href=\"" + Url + "\"><img src=\"img/home.png\" /></a></li>\n";

                for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                {
                    string GroupNewsItemId = (string)dtItems.Rows[iIndex]["GroupNewsItemId"];
                    string GroupNewsItemName = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                    int IsGroupNews = (int)dtItems.Rows[iIndex]["IsGroupNews"];
                    //string NewsByCategoryUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemId, GroupNewsItemName, "_RunTime");
                    if (dtItems.Rows[iIndex]["JumpUrl"].ToString().Trim().Length > 0)
                        Url = (string)dtItems.Rows[iIndex]["JumpUrl"];
                    else if (IsGroupNews == 1)
                        Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemId, GroupNewsItemName, "_RunTime");
                    else Url = "#";
                    // Đúng khi với có category id                                       
                    DataTable dtSub = new FSpecialSubjectBLL().ReadingSubMenuItems(GroupNewsItemId);
                    if (GroupNewsItemId.Equals(Requesid))
                    {
                        tag = true;
                        tabid = iIndex;
                        Html += "<li class=\"has-sub\">" +
                                "<a href=\"" + Url + "\" rel=\"sc" + (iIndex + 1) + "\"><span>" + GroupNewsItemName + "</span></a>";
                        if (dtSub.Rows.Count > 0)
                        {
                            Html += "<ul class=\"sub-ul\">";
                            for (int j = 0; j < dtSub.Rows.Count; j++)
                            {
                                string SubGroupNewsItemId = (string)dtSub.Rows[j]["GroupNewsItemId"];
                                string SubGroupNewsItemName = (string)dtSub.Rows[j][Environments.ActiveLanguage + "GroupNewsItemName"];
                                IsGroupNews = (int)dtSub.Rows[j]["IsGroupNews"];

                                string HtmlContent = "";
                                try
                                {
                                    HtmlContent = (string)dtSub.Rows[j]["HtmlContent"];
                                }
                                catch { }

                                Url = (string)dtSub.Rows[j]["JumpUrl"];
                                if (Url.Trim().Length == 0)
                                {
                                    if (IsGroupNews == 1)
                                        Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName, "_RunTime");
                                    if (IsGroupNews == 0 && HtmlContent.Trim().Length > 0)
                                        Url = Environments.GetStaticHtmlUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName);
                                }
                                Html += "<li><a href=\"" + Url + "\">" + SubGroupNewsItemName + "</a></li>";
                            }
                            Html += "</ul>";
                        }
                        Html += "</li>";
                    }
                    else
                    {
                        if (iIndex == 0)
                        {
                            Html += "<li {0} ><a href=\"" + Url + "\" rel=\"sc" + (iIndex + 1) + "\"><span>" + GroupNewsItemName + "</span></a>";

                        }
                        else
                        {
                            Html += "<li><a href=\"" + Url + "\" rel=\"sc" + (iIndex + 1) + "\"><span>" + GroupNewsItemName + "</span></a>";

                        }
                        if (dtSub.Rows.Count > 0)
                        {

                            Html += "<ul class=\"has-sub\">";
                            for (int j = 0; j < dtSub.Rows.Count; j++)
                            {
                                string SubGroupNewsItemId = (string)dtSub.Rows[j]["GroupNewsItemId"];
                                string SubGroupNewsItemName = (string)dtSub.Rows[j][Environments.ActiveLanguage + "GroupNewsItemName"];
                                IsGroupNews = (int)dtSub.Rows[j]["IsGroupNews"];

                                string HtmlContent = "";
                                try
                                {
                                    HtmlContent = (string)dtSub.Rows[j]["HtmlContent"];
                                }
                                catch { }

                                Url = (string)dtSub.Rows[j]["JumpUrl"];
                                if (Url.Trim().Length == 0)
                                {
                                    if (IsGroupNews == 1)
                                        Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName, "_RunTime");
                                    if (IsGroupNews == 0 && HtmlContent.Trim().Length > 0)
                                        Url = Environments.GetStaticHtmlUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName);
                                }
                                Html += "<li><a href=\"" + Url + "\">" + SubGroupNewsItemName + "</a></li>";
                            }
                            Html += "</ul>";
                        }

                        Html += "</li>";
                    }
                }

                //if (!tag)
                //{
                //    Html = string.Format(Html, " class='active' ");
                //}
                //else
                //    Html = string.Format(Html, string.Empty);
                Html +=
                            "</ul></div>";
                return Html;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            return new FSpecialSubjectBLL().ReadingForService(belongSite, ActiveLanguage);

        }

        [AjaxPro.AjaxMethod()]
        public string GetResultSearchUrl(string belongSite, string Keyword)
        {
            try
            {
                SearchController.CheckValidKeyword(Keyword);
                Keyword = Keyword.Replace(" ", "_");
                if (System.Web.HttpContext.Current.Session["AppId"] == null)
                {
                    System.Web.HttpContext.Current.Session["AppId"] = System.Guid.NewGuid().ToString();
                }
                // TODO: Ask Mr.Quang to fix this function Screen.BuildFrontEndUrl.
                return "ResultSearch.aspx?sKeyword=" + Keyword;
                //return Screen.BuildFrontEndUrl("ResultSearch." + Environments.Extension, "", new ParamCls[]{new ParamCls("sKeyword",Keyword)});
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FMenuWP), ActivePage);
        }

    }
}
